Page({
  data: {
    passport: {},
  },
  onLoad(options) {
    console.log(options)
    // this.getPassport(options.id)
    if (options.id) this.getPassport(options.id)
    if (options.encryptedData) this.getPassportShare(options.encryptedData)
  },
  // 获取通信证信息
  async getPassport(id) {
    let res = await wx.http.get('/visitor/' + id)
    console.log(res)
    this.setData({
      passport: res,
    })
  },
  // 获取分享界面信息
  async getPassportShare(encryptedData) {
    let res = await wx.http.get('/visitor/share/' + encryptedData)
    console.log(res)
    this.setData({ passport: res })
  },
  // 保存二维码
  saveQRCode() {
    // 1.调用图片信息
    wx.getImageInfo({
      src: this.data.passport.url,
      success: (result) => {
        console.log(result)
        // 2.调用API保存图片到相册
        wx.saveImageToPhotosAlbum({
          filePath: result.path,
          success: (result) => {
            console.log(result)
          },
          fail: (error) => {
            console.log(error)
          },
        })
      },
      fail: (err) => {
        console.log(err)
      },
    })
  },
  onShareAppMessage() {
    console.log(1111)
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?encryptedData=' + this.data.passport.encryptedData,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },
})
